import { ClientMessage } from "@runtime/generate/message/ClientMessage";
import WebSocketComponent from "@runtime/module/WebSocket/WebSocketComponent";

/**
 * Light组件
 */
export default class LightComponent {
    private static instance: LightComponent = new LightComponent();
    /**
     * Light组件
     */
    public static get Default(): LightComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    /**
     * 创建灯光
     * @param instanceId 父物体实例Id
     * @param type 灯光类型
     * @param space Transform空间
     * @param position 位置
     * @param eulerAngles 角度
     * @returns 物体实例Id
     */
    public async CreateLightAsync(instanceId: number, type: number, space: number, position: Array<number>, eulerAngles: Array<number>): Promise<number> {
        const response: ClientMessage.CreateLightResponse = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.CreateLightRequest(instanceId, type, space, position, eulerAngles));
        return response.InstanceId;
    }

    /**
     * 更新灯光
     * @param settings 灯光配置
     * @returns 更新结果
     */
    public async UpdateLightAsync(settings: ClientMessage.LightSettings | Array<ClientMessage.LightSettings>): Promise<boolean> {
        const response = await WebSocketComponent.Default.PostMessageAsync(new ClientMessage.UpdateLightRequest(Array.isArray(settings) ? settings : [settings]));
        return response.Error == 0;
    }
}